package com.hsy.linemoney.modules.common;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.LineMoneyContext;

/**
 * Created by MartinKent on 2016/7/11.
 */
public class HowToSaveDialog extends Dialog implements View.OnClickListener {
    protected View topPanel;
    protected View bottomPanel;
    protected View closeBtn;
    private View contentView;
    private boolean isAnimating = false;
    private View rootContainer;
    private TextView line_money_cost;
    private TextView line_money_saved;
    private TextView line_money_rate;
    private TextView line_money_get_price;
    private TextView line_money_get_priceunit;
    private TextView bank_hidden_cost;
    private TextView bank_rate;
    private TextView bank_get_price;
    private TextView bank_get_priceunit;

    public static class HowToSaveDialogData {

        private String linemoneyPayOut;
        private String bankPayOut;
        private String linemoneyTransferFee;
        private String linemoneyRate;
        private String bankTransferFee;
        private String bankRate;
        private String linemoneyTransferFeeUnit;
        private String linemoneyPayOutUnit;
        private String bankTransferFeeUnit;
        private String bankPayOutUnit;

        public String getLinemoneyPayOut() {
            return linemoneyPayOut;
        }

        public String getBankPayOut() {
            return bankPayOut;
        }

        public String getLinemoneyTransferFee() {
            return linemoneyTransferFee;
        }

        public String getLinemoneyRate() {
            return linemoneyRate;
        }

        public String getBankTransferFee() {
            return bankTransferFee;
        }

        public String getBankRate() {
            return bankRate;
        }

        public void setLinemoneyPayOut(String linemoneyPayOut) {
            this.linemoneyPayOut = linemoneyPayOut;
        }

        public void setBankPayOut(String bankPayOut) {
            this.bankPayOut = bankPayOut;
        }

        public void setLinemoneyTransferFee(String linemoneyTransferFee) {
            this.linemoneyTransferFee = linemoneyTransferFee;
        }

        public void setLinemoneyRate(String linemoneyRate) {
            this.linemoneyRate = linemoneyRate;
        }

        public void setBankTransferFee(String bankTransferFee) {
            this.bankTransferFee = bankTransferFee;
        }

        public void setBankRate(String bankRate) {
            this.bankRate = bankRate;
        }

        public String getLinemoneyTransferFeeUnit() {
            return linemoneyTransferFeeUnit;
        }

        public String getLinemoneyPayOutUnit() {
            return linemoneyPayOutUnit;
        }

        public String getBankTransferFeeUnit() {
            return bankTransferFeeUnit;
        }

        public String getBankPayOutUnit() {
            return bankPayOutUnit;
        }

        public void setLinemoneyTransferFeeUnit(String linemoneyTransferFeeUnit) {
            this.linemoneyTransferFeeUnit = linemoneyTransferFeeUnit;
        }

        public void setLinemoneyPayOutUnit(String linemoneyPayOutUnit) {
            this.linemoneyPayOutUnit = linemoneyPayOutUnit;
        }

        public void setBankTransferFeeUnit(String bankTransferFeeUnit) {
            this.bankTransferFeeUnit = bankTransferFeeUnit;
        }

        public void setBankPayOutUnit(String bankPayOutUnit) {
            this.bankPayOutUnit = bankPayOutUnit;
        }
    }

    public HowToSaveDialog(@NonNull Context context, @NonNull HowToSaveDialogData data) {
        super(context, R.style.how_to_save_dialog_style);
        init(context, data);
    }

    private void init(Context context, HowToSaveDialogData data) {
        contentView = LayoutInflater.from(context).inflate(R.layout.how_to_save_dialog, null);
        setContentView(contentView);
        initView(contentView, data);
    }

    private void initView(View rootView, HowToSaveDialogData data) {
        rootContainer = rootView.findViewById(R.id.root_container);
        topPanel = rootView.findViewById(R.id.topPanel);
        bottomPanel = rootView.findViewById(R.id.bottomPanel);

        line_money_saved = (TextView) rootView.findViewById(R.id.line_money_saved);
        line_money_cost = (TextView) rootView.findViewById(R.id.line_money_cost);
        line_money_rate = (TextView) rootView.findViewById(R.id.line_money_rate);
        line_money_get_price = (TextView) rootView.findViewById(R.id.line_money_get_price);
        line_money_get_priceunit = (TextView) rootView.findViewById(R.id.line_money_get_unit);
        bank_hidden_cost = (TextView) rootView.findViewById(R.id.back_hidden_cost);
        bank_rate = (TextView) rootView.findViewById(R.id.back_rate);
        bank_get_price = (TextView) rootView.findViewById(R.id.bank_get_price);
        bank_get_priceunit = (TextView) rootView.findViewById(R.id.bank_get_unit);

        double saved = Double.valueOf(data.getLinemoneyPayOut()) - Double.valueOf(data.getBankPayOut());
        line_money_saved.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f%s", saved, data.getLinemoneyPayOutUnit()));

        line_money_cost.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f%s", Double.valueOf(data.getLinemoneyTransferFee()), data.getLinemoneyTransferFeeUnit()));
        line_money_rate.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.4f", Double.valueOf(data.getLinemoneyRate())));
        line_money_get_price.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f", Double.valueOf(data.getLinemoneyPayOut())));
        line_money_get_priceunit.setText(data.getLinemoneyPayOutUnit());

        bank_hidden_cost.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f%s", Double.valueOf(data.getBankTransferFee()), data.getBankTransferFeeUnit()));
        bank_rate.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.4f", Double.valueOf(data.getBankRate())));
        bank_get_price.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f", Double.valueOf(data.getBankPayOut())));
        bank_get_priceunit.setText(data.getBankPayOutUnit());

        rootView.findViewById(R.id.empty_1).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    dismiss();
                }
                return true;
            }
        });
        rootView.findViewById(R.id.empty_2).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    dismiss();
                }
                return true;
            }
        });
        rootContainer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    dismiss();
                }
                return true;
            }
        });
        topPanel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        bottomPanel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        topPanel.setVisibility(View.VISIBLE);
        bottomPanel.setVisibility(View.INVISIBLE);

        final Animation animShow = AnimationUtils.loadAnimation(getContext(), R.anim.how_to_save_panel_show);
        animShow.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isAnimating = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottomPanel.setVisibility(View.VISIBLE);
                final Animation animShow = AnimationUtils.loadAnimation(getContext(), R.anim.how_to_save_panel_show);
                animShow.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        isAnimating = false;
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                bottomPanel.startAnimation(animShow);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        topPanel.startAnimation(animShow);
        closeBtn = (ImageView) rootView.findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(HowToSaveDialog.this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.close_btn) {
            dismiss();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (isAnimating) {
            return;
        }
        super.show();
    }

    @Override
    public void dismiss() {
//        super.dismiss();
        if (isAnimating) {
            return;
        }
        final Animation animHide = AnimationUtils.loadAnimation(getContext(), R.anim.how_to_save_panel_hide);
        animHide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isAnimating = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottomPanel.setVisibility(View.INVISIBLE);
                final Animation animHide = AnimationUtils.loadAnimation(getContext(), R.anim.how_to_save_panel_hide);
                animHide.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        topPanel.setVisibility(View.INVISIBLE);
                        hideDialog();
                        isAnimating = false;
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                topPanel.startAnimation(animHide);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        bottomPanel.startAnimation(animHide);
    }

    private void hideDialog() {
        super.dismiss();
    }
}
